﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - Classroom" 
*/ 

component 
	displayname="public.service.db.senate.sequence.Classroom" 
	output=false 
	accessors=true 
	{

	property type="string" name="BuildingID" hint="建筑";
	property type="string" name="Floor" hint="楼层";
	
	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = getBuildingID() & getFloor() & numberFormat(1, "0___");
		var sql = "	SELECT
						c.cr_id
					FROM
						t_classroom c
					WHERE
						c.b_id = :bid 
						AND
						c.cr_floor = :floor 
						AND
						c.cr_id LIKE :condition 
					ORDER BY
						c.cr_id DESC";

		var queryObj = new Query(datasource = application.dnsMaster);
		queryObj.addParam(name = "bid", value = getBuildingID(), cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "floor", value = getFloor(), cfsqltype = "cf_sql_char");
		queryObj.addParam(name = "condition", value = getBuildingID() & getFloor() & "____", cfsqltype = "cf_sql_char");
		var rs_check = queryObj.execute( sql = sql ).getResult();
		
		/* 
			主键生成规则 CHAR(12)
		
			6位楼宇代码 + 2位楼层标识 + 4位为流水号
			
			其中所在楼层标识 01 表示 第一楼, B1 表示 地下1楼 地下标识可能用于地势复杂区域 存在入口在楼宇中央的情景
			
		*/
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = getBuildingID() & getFloor() & numberFormat((right(rs_check.cr_id, 4) + 1), "0___");
			
		}

		return nextID;
	}

}

</cfscript>